package com.hitemole.manager;

import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.List;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.hitemole.game.GameCore;
import com.hitemole.network.ClientGame;

public class MenuManager {
	public enum CONNECTION_STATE{NOTHING,STARTCLIENT,WAITCLIENT,CLIENTCONNECTED,WAITSERVER,STARTSERVER,SERVERCONNECTED};
	private CONNECTION_STATE state;
	private String[] server = {};
	private GameCore core;
	private Stage stage;
	private Table table,table2,serverListWrapper,labelWrapper;
	private TextButton startButton,multiplayerButton,highscoreButton,joinButton,hostButton;
	private Label waitingLabel;
	private Image background;
	private Image[] clouds;
	private List serverList;
	public MenuManager(GameCore core) {
		// TODO Auto-generated constructor stub
		this.state=CONNECTION_STATE.NOTHING;
		this.core=core;
		this.stage = new Stage(GameManager.DEFAULT_SCREEN_HEIGHT, GameManager.DEFAULT_SCREEN_HEIGHT,false);
//		stage = new Stage();
		table = new Table(core.getResourceManager().skinAsset.skin);
		table.setPosition(stage.getWidth()+50, table.getY());
		table2 = new Table(core.getResourceManager().skinAsset.skin);
		clouds = new Image[4];
		for(int i=0;i<4;i++){
			clouds[i] = new Image(core.getResourceManager().backgroundAsset.cloudTexture);
		}
		
		clouds[0].setPosition(250, 700);
		clouds[0].setOrigin(0-clouds[0].getX()+350, 0-clouds[0].getY());
		clouds[0].addAction(Actions.rotateBy(-3600, 500));
		
		
		clouds[1].setPosition(0, 560);
		clouds[1].setOrigin(clouds[1].getWidth()/2, clouds[1].getHeight()/2);
		clouds[1].rotate(90);
		clouds[1].setOrigin(0-clouds[1].getX()+350, 0-clouds[1].getY());
		clouds[1].addAction(Actions.rotateBy(-3600, 500));
		
		clouds[2].setPosition(0, 650);
		clouds[2].setOrigin(clouds[2].getWidth()/2, clouds[2].getHeight()/2);
		clouds[2].rotate(-90);
		clouds[2].setOrigin(0-clouds[2].getX()+350, 0-clouds[2].getY());
		clouds[2].addAction(Actions.rotateBy(-3600, 500));
		
		clouds[3].setPosition(250, 550);
		clouds[3].setOrigin(clouds[3].getWidth()/2, clouds[3].getHeight()/2);
		clouds[3].rotate(180);
		clouds[3].setOrigin(0-clouds[3].getX()+350, 0-clouds[3].getY());
		clouds[3].addAction(Actions.rotateBy(-3600, 500));
		
		Table buttonWrapper = new Table();
		startButton = new TextButton("Start", core.getResourceManager().skinAsset.skin);
		startButton.addListener(new InputListener() {
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				// TODO Auto-generated method stub
				background.addAction(Actions.fadeOut(0.5f));
				for(int i=0;i<clouds.length;i++){
					clouds[i].addAction(Actions.fadeOut(0.5f));
				}
				table2.addAction(Actions.sequence(Actions.fadeOut(0.5f),new Action(){

					@Override
					public boolean act(float delta) {
						// TODO Auto-generated method stub
						MenuManager.this.core.restartGame();
						MenuManager.this.core.setScreen(MenuManager.this.core.getGameScreen());
						return true;
					}
					
				}));
				startButton.setChecked(false);
				super.touchUp(event, x, y, pointer, button);
			}

			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				// TODO Auto-generated method stub
				return true;
			}
		});
		highscoreButton = new TextButton("Highscore", core.getResourceManager().skinAsset.skin);
		multiplayerButton = new TextButton("Multiplayer",core.getResourceManager().skinAsset.skin);
		multiplayerButton.addListener(new InputListener() {
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				// TODO Auto-generated method stub
				table2.addAction(Actions.moveTo(-stage.getWidth(), table2.getY(),0.3f));
				table.addAction(Actions.sequence(Actions.delay(0.15f),Actions.moveTo(0, 0,0.3f)));
				super.touchUp(event, x, y, pointer, button);
			}
			
			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				// TODO Auto-generated method stub
				return true;
			}
		});
		table.setFillParent(true);
		table.top();
		table2.setFillParent(true);
		buttonWrapper.add(startButton).uniform().fill().spaceBottom(30).spaceTop(100);
		buttonWrapper.row();
		buttonWrapper.add(multiplayerButton).uniform().fill().spaceBottom(30);
		buttonWrapper.row();
		buttonWrapper.add(highscoreButton).uniform().fill().spaceBottom(30);
		buttonWrapper.setTransform(true);
//		buttonWrapper.setOrigin(-10, 100);
		buttonWrapper.setOrigin(buttonWrapper.getWidth()/2, buttonWrapper.getHeight()/2);
		buttonWrapper.setScale(0.75f);
//		buttonWrapper.pad(20);
		table2.top();
		startButton.scale(0.2f);
		background = new Image(core.getResourceManager().backgroundAsset.mainmenuTexture);
		table2.add(buttonWrapper);
		table2.row();
//		table.stack(new Image(background),table2);
		
	
		stage.addActor(background);
		stage.addActor(clouds[0]);
		stage.addActor(clouds[1]);
		stage.addActor(clouds[2]);
		stage.addActor(clouds[3]);
		setMultiplayerMenu();
		stage.addActor(table2);
		stage.addActor(table);
	}
	
	public void setMultiplayerMenu(){
		serverListWrapper = new Table(core.getResourceManager().skinAsset.skin);
		labelWrapper = new Table(core.getResourceManager().skinAsset.skin);
		serverListWrapper.addAction(Actions.fadeOut(0));
		labelWrapper.addAction(Actions.fadeOut(0));
		
		serverListWrapper.setBackground("table");	
		
		Label multiplayerLabel= new Label("Multiplayer",core.getResourceManager().skinAsset.skin,"title-text");
		multiplayerLabel.setFontScale(1.5f);
		waitingLabel= new Label("Waiting for Client \n to Connect...",core.getResourceManager().skinAsset.skin,"normal-text");	
		serverList = new List(server, core.getResourceManager().skinAsset.skin);
		serverList.addListener(new InputListener(){
			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				// TODO Auto-generated method stub
				ClientGame.clientGame.connectTo(serverList.getSelectedIndex());
				return super.touchDown(event, x, y, pointer, button);
			}
		});
		table.add(multiplayerLabel).colspan(2).spaceBottom(10);
		hostButton = new TextButton("Host", core.getResourceManager().skinAsset.skin);
		joinButton = new TextButton("Join", core.getResourceManager().skinAsset.skin);
		joinButton.addListener(new InputListener(){
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				// TODO Auto-generated method stub
				setState(CONNECTION_STATE.STARTCLIENT);
				serverListWrapper.addAction(Actions.fadeIn(0.5f));
				hostButton.setChecked(false);
				super.touchUp(event, x, y, pointer, button);
			}
			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				// TODO Auto-generated method stub
				return true;
			}
		});
		hostButton.addListener(new InputListener(){
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				// TODO Auto-generated method stub
				setState(CONNECTION_STATE.STARTSERVER);
				serverListWrapper.addAction(Actions.fadeOut(0.5f));
				labelWrapper.addAction(Actions.fadeIn(0.5f));
				joinButton.setChecked(false);
				super.touchUp(event, x, y, pointer, button);
			}
			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				// TODO Auto-generated method stub
				return true;
			}
		});
		table.row();
		table.add(hostButton).uniform().spaceRight(10).spaceBottom(20);
		table.add(joinButton).spaceBottom(20);
		table.row().size(300, 400);
		serverListWrapper.add(serverList).fillX().expand().top();
		labelWrapper.add(waitingLabel).fill();
		table.stack(labelWrapper,serverListWrapper).colspan(2);
	}
	
	public Stage getStage(){
		return stage;
	}
	
	public GameCore getGameCore() {
		return core;
	}

	public Table getTable() {
		// TODO Auto-generated method stub
		return table;
	}
	
	public TextButton getJoinButton(){
		return joinButton;
	}
	
	public TextButton getHostButton() {
		return hostButton;
	}
	
	public void setServerList(String[] server){
		this.serverList.setItems(server);
	}

	public CONNECTION_STATE getState() {
		return state;
	}

	public void setState(CONNECTION_STATE state) {
		this.state = state;
	}
	
	public Label getWaitingLabel(){
		return waitingLabel;
	}
}
